﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using TomNet.IMSC.Core.Contracts.Common;
using TomNet.IMSC.Core.Contracts.NewHouse;
using TomNet.IMSC.Model.DbModels.NewHouse;
using TomNet.IMSC.Web.Common;
using TomNet.IMSC.Web.Filters;
using TomNet.IMSC.Web.Models.StaffApiParams;
using TomNet.Web.Mvc.UI;
using TomNet.Web.WebApi;

namespace TomNet.IMSC.Web.Controllers.StaffAPI
{
    [Description("API-新房")]
    [ApiAuthentication]
    public class StaffNewHouseController : BaseApiController
    {

        public IBuildingsContract BuildingsContract { get; set; }
        public IAreaContract AreaContract { get; set; }
        public IBuildingsStatisticsContract BuildingsStatisticsContract { get; set; }
        public IPropertyConsultantContract PropertyConsultantContract { set; get; }

        public IBuildingBuildingTypeContract BuildingBuildingTypeContract { get; set; }



        //新房====================================================================================================

        [Description("API-新房列表")]
        public IHttpActionResult ttGetBuildingsList([FromBody]BuildingsParamsModel entity)
        {

            var query = BuildingsContract.Entities.Where(m => m.IsDeleted == false);

            //条件搜索
            if (!string.IsNullOrWhiteSpace(entity.KeyWord))
            {
                query = query.Where(m => m.Name.Contains(entity.KeyWord));
            }
            if (entity.BuildingTypeID != 0)
            {
                query = query.Where(m => BuildingBuildingTypeContract.Entities.Count(p => p.BuildingID == m.Id && p.BuildingTypeID == entity.BuildingTypeID) > 0);
            }

            var list = (from m in query
                        join n in BuildingsStatisticsContract.Entities
                        on m.Id equals n.BuildingID into temp
                        from tt in temp.DefaultIfEmpty()
                        let areaName = AreaContract.Entities.Where(m1 => m1.Id == m.AreaID).Select(m1 => m1.Name).FirstOrDefault()//区域名称
                        //let img = (from m1 in ImagesContract.Entities where m1.GroupID == 1 && m1.InterfixID == m.Id && m1.IsDeleted == false orderby m1.Id select (m1.ImagePath + m1.FileName)).FirstOrDefault()
                        select new BuildingsListModel
                        {
                            Id = m.Id,
                            Name = m.Name,
                            AreaName = (areaName == null) ? "" : areaName,
                            Address = m.Address,
                            BuildingInfo = m.BuildingInfo,
                            CreatedTime = m.CreatedTime,

                            FilingNumber_Total = (tt == null) ? 0 : tt.FilingNumber_Total,
                            FilingNumber_30Day = (tt == null) ? 0 : tt.FilingNumber_30Day,
                            FilingNumber_7Day = (tt == null) ? 0 : tt.FilingNumber_7Day,

                            SuccessNumber_Total = (tt == null) ? 0 : tt.SuccessNumber_Total,
                            SuccessNumber_30Day = (tt == null) ? 0 : tt.SuccessNumber_30Day,
                            SuccessNumber_7Day = (tt == null) ? 0 : tt.SuccessNumber_7Day


                        });

            //数据总数
            var total = list.Count();

            //默认给一个排序的字段
            list = list.OrderByDescending(m => m.CreatedTime);

            //分页(假如total == 0，则取消分页查询，提高性能)
            list = total > 0 ? list.Skip((entity.PageIndex - 1) * entity.PageSize).Take(entity.PageSize)
                : Enumerable.Empty<BuildingsListModel>().AsQueryable();// null;


            var result = new AjaxResult("成功", AjaxResultType.Success, list.ToList());
            return Token(result, "");

        }



        [Description("API-置业顾问")]
        public IHttpActionResult ttPropertyConsultantList([FromBody]PropertyConsultantParamsModel entity)
        {
            var query = PropertyConsultantContract.Entities.Where(m => m.BuildingID == entity.BuildingID && m.IsDeleted == false);//置业顾问

            //条件搜索
            if (!string.IsNullOrWhiteSpace(entity.KeyWord))
            {
                query = query.Where(m => m.Name.Contains(entity.KeyWord));
            }

            var list = query;

            //数据总数
            var total = list.Count();

            //默认给一个排序的字段
            list = list.OrderByDescending(m => m.CreatedTime);

            //分页(假如total == 0，则取消分页查询，提高性能)
            list = total > 0 ? list.Skip((entity.PageIndex - 1) * entity.PageSize).Take(entity.PageSize)
                : Enumerable.Empty<PropertyConsultant>().AsQueryable();// null;


            var result = new AjaxResult("成功", AjaxResultType.Success, list.ToList());
            return Token(result, "");

        }


        [Description("API-置业顾问添加")]
        public IHttpActionResult ttInsertNewPropertyConsultant([FromBody]PropertyConsultant entity)
        {

            if (entity == null)
            {
                var result = new AjaxResult("添加失败", AjaxResultType.Error);
                return Token(result, "");
            }

            try
            {
                //获取StaffID
                int staffid = BaseWebApiExt.GetStaffIDByToken();
                entity.CreateStaffID = staffid;

                //新增
                int count = PropertyConsultantContract.Insert(entity);

                if (count > 0)
                {
                    var result = new AjaxResult("添加成功", AjaxResultType.Success);
                    return Token(result, "");
                }
                else
                {
                    var result = new AjaxResult("添加失败", AjaxResultType.Error);
                    return Token(result, "");
                }
            }
            catch (Exception ex)
            {
                var result = new AjaxResult("添加异常", AjaxResultType.Error);
                return Token(result, "");
            }

        }


    }
}
